General

Components

Community

Development

TDF

Documents > Cookbook >Manipulate TextSearch



TextNavigation
First an ODF text document is needed to test the navigation operation. The following codes shows two main functions of TextNavigation :hasNext() and getCurrentItem(). The first parameter of the TextNavigation constructor is the matched pattern String, and the second is the navigation scope.
The result of function getCurrentItem is a Selection object, so a TextSelection object is used here to check out the result.Finally the informations of all the String "What" in the text document will be printed out.

		TextDocument textdoc=(TextDocument)TextDocument.loadDocument("textsearch.odt");
TextNavigation search1;
search1=new TextNavigation("What",textdoc);
while (search1.hasNext()) {
TextSelection item1 = (TextSelection) search1.nextSelection();
System.out.println(item1);
}


TextSelection

Get Index/Text of TextSelection
Run the following codes will get the text content of the searched String "good" and the corresponding index in the text document.

		TextNavigation search2=new TextNavigation("good",textdoc);
while(search2.hasNext()){
TextSelection item2=(TextSelection) search2.nextSelection();
String searchedText=item2.getText();
int searchedIndex=item2.getIndex();
System.out.println(searchedText);
System.out.println(searchedIndex);
}

Cut String
To cut some specified string in a text document, you can do like the following codes which cut off all the String "day" in the document.

	        search2=new TextNavigation("day",textdoc);
while(search2.hasNext()){
TextSelection item=(TextSelection) search2.nextSelection();
item.cut();
}

Paste String
The following codes paste the string "change" both at the front and at the end of the string "good", by using the function pasteAtFrontOf() and pasteAtEndOf().

		search2 = null;
search2 = new TextNavigation("good", textdoc);
TextSelection pastesource = null;
TextNavigation search3 = new TextNavigation("change", textdoc);
if (search3.hasNext()) {
pastesource = (TextSelection) search3.nextSelection();
}
while (search2.hasNext()) {
TextSelection item = (TextSelection) search2.nextSelection();
//paste "change" at the front of "good" pastesource.pasteAtFrontOf(item);
//paste "change" at the end of "good" pastesource.pasteAtEndOf(item);
}

Replace String with String
The following codes replace all the string "replacesource" with the string "replacedest" in the text document.

		search2 = null;
search2 = new TextNavigation("replacesource", textdoc);
while (search2.hasNext()) {
TextSelection item= (TextSelection) search2.nextSelection();
item.replaceWith("replacedest");
}

Replace String with Paragraph
The following codes replace all the string "replacesource" with a paragraph which can be in the same document or in a different document.

		search2 = null;
search2 = new TextNavigation("replacesource", textdoc);
while (search2.hasNext()) {
TextSelection item= (TextSelection) search2.nextSelection();
Paragraph paragraph = textdoc.getParagraphByIndex(0, true);
item.replaceWith(paragraph);
}

Replace String with Image
The following codes replace all the string "replacesource" with an image. There are two ways to call the replaceWith method. You can use an image URI as parameter.

		search2 = null;
search2 = new TextNavigation("replacesource", textdoc);
while (search2.hasNext()) {
TextSelection item= (TextSelection) search2.nextSelection();
//Use URI as parameter. item.replaceWith(new URI("image.png"));
}
Or you can use an Image object as below:

			//Use Image as parameter.
			item.replaceWith(Image.newImage(para, new URI("image.png")));

Replace String with Table
The following codes replace all the string "replacesource" with a table named "myTable" in the text document.

		search2 = null;
Table table = textdoc.getTableByName("myTable");
search2 = new TextNavigation("replacesource", textdoc);
while (search2.hasNext()) {
TextSelection item= (TextSelection) search2.nextSelection();
item.replaceWith(table);
}

Replace String with Field
The following codes replace all the string "replacesource" with a field named "myField" in the text document.

		search2 = null;
Field field = textdoc.getVariableFieldByName("myField");
search2 = new TextNavigation("replacesource", textdoc);
while (search2.hasNext()) {
TextSelection item= (TextSelection) search2.nextSelection();
item.replaceWith(field);
}

Replace String with TextDocument
The following codes replace all the string "replacesource" with the contents of the text document named replacedest.odt.

		search2 = null;
TextDocument destDoc=TextDocument.loadDocument("replacedest.odt");
search2 = new TextNavigation("replacesource", textdoc);
while (search2.hasNext()) {
TextSelection item= (TextSelection) search2.nextSelection();
item.replaceWith(destDoc);
}

Add Reference to String
To add reference for a string, you can do like the following codes. Here function addHref is used, the parameter of it is an URL object. The codes add network address "http:www.ibm.com" to the string "network".

		search2 = null;
search2 = new TextNavigation("network", textdoc);
while (search2.hasNext()) {
TextSelection item = (TextSelection) search2.nextSelection();
item.addHref(new URL("http://www.ibm.com"));
}

Add Comment
Adding comment is a useful function when review document, such as spell check and security check. You can do it like the following codes. Here, function addComment is used, the first parameter is the comment content, the second parameter is the comment author. The codes add a spell suggestion before the string "natwork".

		TextNavigation search4 = new TextNavigation("natwork", textdoc);
while (search4.hasNext()) {
TextSelection selection = (TextSelection) search4.nextSelection();
selection.addComment("Please change 'natwork' with 'network'.", "SpellChecker");
}


FieldSelection
Field Selection is a decorator class of TextSelection, which help user replace a text content with field. Following code can be used to search the document content, and replace with a simple field.

		TextDocument doc = TextDocument.loadDocument("fieldSample.odt");
TextNavigation search = new TextNavigation("ReplaceDateTarget", doc);
while (search.hasNext()) {
TextSelection item = (TextSelection) search.nextSelection();
FieldSelection fieldSelection = new FieldSelection(item);
fieldSelection.replaceWithSimpleField(Field.FieldType.FIXED_DATE_FIELD);
}

Following code can be used to search the document content, and replace with a condition field.

		TextSelection item = (TextSelection) search.nextSelection();
FieldSelection fieldSelection = new FieldSelection(item);
fieldSelection.replaceWithConditionField("test_con_variable == \"true\"", "trueText", "falseText");

Following code can be used to replace with a hidden field.

		fieldSelection.replaceWithHiddenTextField("test_con_variable == \"true\"", "hiddenText");

Following code can be used to replace with a reference field.

		ReferenceField referenceField = Fields.createReferenceField(doc.addParagraph("span").getOdfElement(), "selection-test-ref");
fieldSelection.replaceWithReferenceField(referenceField, ReferenceField.DisplayType.TEXT);

Following code can be used to replace with a variable field.

		VariableField userVariableField = Fields.createUserVariableField(doc, "selection_user_variable", "test");
fieldSelection.replaceWithVariableField(userVariableField);


TextStyleNavigation
Similar with TextNavigation, TextStyleNavigation has two main functions: getCurrentItem() and hasNext() which is shown in the following codes. The input parameter of TextStyleNavigation constructor is a map of OdfStyleProperty, so here a TreeMap "searchProps" which contains the Style properties is used to construct the TextStyleNavigation object.

		TextStyleNavigation searchStyle1;
TreeMap<OdfStyleProperty, String> searchProps = new TreeMap<OdfStyleProperty, String>();
searchProps.put(StyleTextPropertiesElement.FontName, "Times New Roman1");
searchProps.put(StyleTextPropertiesElement.FontSize, "16pt");
searchStyle1 = new TextStyleNavigation(searchProps, textdoc);
if (searchStyle1.hasNext()) {
TextSelection itemstyle = (TextSelection) searchStyle1.nextSelection();
System.out.print((itemstyle.toString()));
}


Impressum (Legal Info) | Privacy Policy (Datenschutzerklärung) | Statutes (non-binding English translation) - Satzung (binding German version) | Copyright information: Unless otherwise specified, all text and images on this website are licensed under the Apache License, v2.0. This does not include the source code of LibreOffice, which is licensed under the Mozilla Public License v2.0. “LibreOffice” and “The Document Foundation” are registered trademarks of their corresponding registered owners or are in actual use as trademarks in one or more countries. Their respective logos and icons are also subject to international copyright laws. Use thereof is explained in our trademark policy. LibreOffice was based on OpenOffice.org.